﻿using System;
using System.Windows.Forms;
using LayerEntity.Persona;
using LayerEntity.Sistema;
namespace System2012.Persona
{
    public partial class BuscarContacto :  DevExpress.XtraEditors.XtraForm 
    {
        public BuscarContacto()
        {
            this.InitializeComponent();
        }

        #region Variables y Propiedades
        public bool Estado { get; set; }
        EContacto c = null;
        public EContacto Contacto { get {
            return c;
        } }
        #endregion

        #region Metosos
        void SeleccionarContacto() {
            if (gridView1.RowCount > 0)
            {
                EContacto con = (EContacto)gridView1.GetFocusedRow();
                if (con != null) {
                    this.c = con;
                    this.Estado = true;
                    Close();
                }
            }
        }
        #endregion

        private void BgwLoadContactosDoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            
            BGWLoadContactos.ReportProgress(20, "Load Contactos");
            e.Result = SystemUtils.RecordManagerPersona.GetContactos(e.Argument.ToString());
            BGWLoadContactos.ReportProgress(100, "Load Complete");
        }

        private void BgwLoadContactosProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            progressBarControl1.EditValue = e.ProgressPercentage;
            lblProgreso.Text = e.UserState + ":";
        }

        private void BgwLoadContactosRunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            gridControl1.DataSource = e.Result;
            gridControl1.RefreshDataSource();
            lblProgreso.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Never;
        }

        private void GridView1CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column.Name == "gcDocumento")
            {
                object obj = gridView1.GetRowCellValue(e.RowHandle, gcIdDocumento );
                if (obj != null)
                {
                    EDocumento doc = SystemUtils.RecordManagerCommonList.GetDocument(Convert.ToInt64(obj.ToString()));
                    if (doc != null)
                    {
                        e.DisplayText = doc.Abreviatura;
                    }
                }
            }
            
            if (e.Column.Name == "gcEstado")
            {
                object obj = gridView1.GetRowCellValue(e.RowHandle, gcIdEstado);
                if (obj != null)
                {
                    EEstado est = SystemUtils.RecordManagerCommonList.GetEstadoById(Convert.ToInt32(obj.ToString()));
                    if (est != null)
                    {
                        e.DisplayText = est.Descripcion;
                    }
                }
            }
            if (e.Column.Name == "gcDesUbigeo")
            {
                object obj = gridView1.GetRowCellValue(e.RowHandle, gcUbigeo);
                if (obj != null) {
                    EUbigeo ubi = SystemUtils.RecordManagerCommonList.GetUbigeoByUbigeo(obj.ToString());
                    if (ubi != null ) {
                        e.DisplayText = ubi.ToString();
                    }
                }
            }
        }

        private void GridView1DoubleClick(object sender, EventArgs e)
        {
            SeleccionarContacto();
        }

        private void GridView1KeyPress(object sender, KeyPressEventArgs e)
        {
            SeleccionarContacto();
        }

        private void SimpleButton1Click(object sender, EventArgs e)
        {
            if (!BGWLoadContactos.IsBusy) {
                string filtro = TxtBuscar.EditValue.ToString().Trim();
                if (!string.IsNullOrEmpty(filtro)) {
                    switch (comboBoxEdit1 .SelectedIndex )
                    {
                        case 0:
                            filtro = " where doc_contacto ILIKE '%" + filtro + "%'";
                            break;
                        case 1:
                            filtro = " where nom_contacto ILIKE '%" + filtro + "%'";
                            break;
                        default:
                            break;
                    }
                }
                filtro = filtro.Trim();
                BGWLoadContactos.RunWorkerAsync( filtro);
            }
        }
    }
}